<?php
namespace app\admin\model;

use think\Exception;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\Model;
//商品页
class Goods extends Model{
    //定义表主键
    protected $pk = 'goods_id';
    //商品列表页
     public static function   getGoodsList($order,$data,$where){
         $list=Goods::alias('g')
             ->field('g.goods_id,g.goods_name,g.default_image,g.price,b.brand_name,c.cate_name,g.add_time,g.recommended,g.recom,g.if_show')
             ->where($where)
             //->withSearch(array_keys($datas),$datas)
             ->join('ecm_brand b','g.brand=b.brand_id')
             ->join('ecm_gcategory c','g.cate_id=c.cate_id')
             ->order($order)->paginate([
                 'list_rows'=> 5,
                 'query'=>$data
             ]);
         return $list;
     }
     //搜索器
//     public static function   searchKeywordsAttr($query,$value,$data){
//         $query->where('g.goods_name','like','%'.$value.'%');
//     }
//    public static function   searchIfShowAttr($query,$value,$data){
//         $query->where('g.if_show','=',$value);
//    }
//    public static function   searchRecommendedAttr($query,$value,$data){
//        $query->where('g.recommended','=',$value);
//    }
//    public static function   searchPinpaiAttr($query,$value,$data){
//        $query->where('g.brand','=',$value);
//    }
//    public static function   searchMintimeAttr($query,$value,$data){
//        $query->whereTime('g.add_time','>',$value);
//    }
//    public static function   searchMaxtimeAttr($query,$value,$data){
//        $query->whereTime('g.add_time','<',$value);
//    }
//    public static function   searchMinmoneyAttr($query,$value,$data){
//        $query->where('g.price','>',$value);
//    }
//    public static function   searchMaxmoneyAttr($query,$value,$data){
//        $query->where('g.price','<',$value);
//    }
//    public static function   searchCidAttr($query,$value,$data){
//        $query->where('g.cate_id','=',$value);
//    }
    //查询品牌下拉处理
       public static function   goodsPin(){
           $pin=Brand::field('brand_id,brand_name')->select();
           return $pin;
       }
      //查询分类下拉处理
        public static function  gcate(){
            $cate=Gcategory::field("cate_id,cate_name,parent_id")->select()->toArray();
                return $cate;
            }
    public static function   getIfShowImgAttr($v,$data){
        if ($data['if_show']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }
    public static function   getRecommendedImgAttr($v,$data){
        if ($data['recommended']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }
    public static function   getRecomImgAttr($v,$data){
        if ($data['recom']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }
     public static function   getAddTimeTextAttr($v){
             return date('Y-m-d H:i:s',$v);
     }

     //商品添加
     public static function   AddGoodsInfo($data){
         try{
             $res=Goods::strict(true)->insert($data);
         }catch (Exception $e){
             $res=false;
         }
         return $res;
     }

    //商品修改
     public static function   goodEditInfo($data,$get){
         if (Request::isPost()){
             try{
                 $res=Goods::update($data);

             }catch (Exception $e){
                 $res=false;
             }
             return $res;
         }else{

         }
        $find=Goods::where('goods_id','=',$get['id'])->find()->toArray();
        return $find;

     }





}